import json
from crawler.extractors import BaseExtractor, JsonpExtractor
from crawler.fields import ContainerField, TextField, AttrField, JsonField

__all__ = ['ZapposMainExtractor', 'ZapposDetailMainExtractor']


class ZapposMainInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.products.list[*]')
    title = JsonField(json_selector='$.productName')
    spu_id = JsonField(json_selector='$.productId')
    brand = JsonField(json_selector='$.brandName')
    cost_price = JsonField(json_selector='$.price')
    retail_price = JsonField(json_selector='$.originalPrice')
    url = JsonField(json_selector='$.productUrl')
    color = JsonField(json_selector='$.color')
    # color_id = JsonField(json_selector='$.colorId')


class ZapposDetailInfoExtractor(JsonpExtractor):
    product_des = JsonField(json_selector='$.product.detail.description.bulletPoints')
    specifications = JsonField(json_selector='$.product.detail.description.sizeCharts')
    size_stock = JsonField(json_selector='$.product.detail.sizing.allUnits[0].values', many=True)


class ZapposSizeStockInfoExtractor(JsonpExtractor):
    container = ContainerField(json_selector='$.product.detail.styles[*]')
    cost_price = JsonField(json_selector='$.price')
    retail_price = JsonField(json_selector='$.originalPrice')
    color = JsonField(json_selector='$.color')
    # size = JsonField(json_selector='$.originalPrice')
    stocks = JsonField(json_selector='$.stocks[*]', many=True)
    image_list = JsonField(json_selector='$.images[*]', many=True)
    colorId = JsonField(json_selector='$.colorId', many=True)


class ZapposMainExtractor:
    @classmethod
    def get_item(cls, response, res):
        return cls._extract(response, res)

    @classmethod
    def _extract(cls, response, res):
        task = response.meta['task']
        sex = task['sex']
        category = task['category']
        info_list = list()
        for item in ZapposMainInfoExtractor.get_items(response, res):
            info_dict = dict()
            info_dict['spu_id'] = item.spu_id
            # 品牌
            info_dict['brand'] = item.brand
            # 商标题
            info_dict['title'] = item.title
            # 品类
            info_dict['category'] = category
            # info_dict['color'] = item.color
            # info_dict['color_id'] = item.color_id
            # 性别
            info_dict['sex'] = sex
            # 图集 需要请求详情页
            # info_dict['atlas'] = ''
            # 成本价 原价对应公价
            info_dict['cost_price'] = item.cost_price.split('$')[1]
            # 公价
            info_dict['retail_price'] = item.retail_price.split('$')[1]
            # 货币单位
            info_dict['currency'] = 'USD'

            info_dict['url'] = 'https://www.zappos.com' + item.url if item.url else ''
            # 规格 ，
            info_dict['specifications'] = ''
            # 商品描述。详情页的
            info_dict['product_des'] = ''
            # info_dict['size_stock'] = ''
            # 尺码表
            info_list.append(info_dict)

        return info_list


class ZapposDetailMainExtractor:
    @classmethod
    def get_item(cls, response, res):
        return cls._extract(response, res)

    @classmethod
    def _extract(cls, response, res):
        item = ZapposDetailInfoExtractor.get_item(response, res)
        info_dict = dict()
        info_dict['product_des'] = ','.join(item.product_des)
        for i in item.product_des:
            if '%' in i:
                info_dict['composition'] = i
                break
            else:
                info_dict['composition'] = ''

        for info in ZapposSizeStockInfoExtractor.get_items(response, res):
            color_id = info.colorId[0]
            if color_id == response.url.split('/')[-1]:
                size_stock_list = list()
                for i in info.stocks:
                    stock_dict = dict()
                    stock_dict['size'] = i['size'] + str(i.get('width'))
                    stock_dict['stock'] = i['onHand']
                    size_stock_list.append(stock_dict)
                info_dict['size_stock'] = size_stock_list
                info_dict['color'] = info.color
                info_dict['atlas'] = [
                    'https://m.media-amazon.com/images/I/{}._AC_SR768,1024__FMwebp_.jpg'.format(_['imageId']) for _ in
                    info.image_list]
                break
            # detail_info_list.append(info_dict)
        return info_dict
